package com.xuchaotong.arr;

import lombok.extern.slf4j.Slf4j;
import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Slf4j
public class ArrayOperationTest {


    @Test
    public void test() {
        String[] promotionArr = new String[]{"453", "8", "41419"};

        List<String[]> arr = buildArr(promotionArr);

        for (String[] item : arr) {
            log.info("数组：{}", Arrays.toString(item));
        }
    }


    public static List array2d(List list) {
        List retList = new ArrayList();
        int max = 2 << (list.size() - 1);
        for (int i = 1; i < max; i++) {
            String[] dim = new String[list.size()];
            int temp = i;
            for (int j = list.size(); j > 0; j--) {
                if (temp >= Math.pow(2, j - 1)) {
                    dim[list.size() - j] = String.valueOf(list.get(list.size() - j));
                    temp = temp - (int) Math.pow(2, j - 1);
                } else {
                    dim[list.size() - j] = "-1";
                }
            }
            retList.add(dim);
        }
        return retList;
    }


    public List<String[]> buildArr(String[] templateArr) {
        List<String[]> reArr = new ArrayList<>();
        int len = templateArr.length;
        int max = (int) Math.pow(2, len);

        for (int i = 1; i < max; i++) {
            String[] dim = new String[len];
            int temp = i;
            for (int j = len; j > 0; j--) {
                if (temp >= Math.pow(2, j - 1)) {
                    dim[len - j] = templateArr[len - j];
                    temp = temp - (int) Math.pow(2, j - 1);
                } else {
                    dim[len - j] = "-1";
                }
            }
            reArr.add(dim);
        }

        return reArr;
    }
}
